
var menu = document.getElementById('allmenu');
var shopCart = document.getElementById('shopcart');
var table = document.getElementById('list');

var tbody = document.getElementById('listBody');
var num = document.getElementById('num');
var account = shopCart.getElementsByClassName('account');
var noth = document.getElementById('null');

var rel = document.getElementById('money');
//空购物车是否为0？
function or() {
    if (num.innerText == 0) {
        account[0].style.display = 'none';
        noth.style.display = 'block';
    }
}

window.onload = function () {
    for (var i = 0; i < tbody.rows.length; i++) {
        if (tbody.rows[i].cells[0].children[0].checked) {
            //合计
            rel.innerText = Number(rel.innerText) + tbody.rows[i].cells[2].innerText * tbody.rows[i].cells[3].innerText;
        }
    }
}


// 加入购物车
function add(foodName, foodPrice) {
    console.log(foodName, foodPrice);
    //从本地存储获得购物车的数据
    var cartStr = localStorage.getItem('cart');
    var cart = JSON.parse(cartStr);
    //判断原有购物车是否为空
    if (cart == null) {
        cart = [];
    }
    //判断是否已经点过这个菜
    for (var i = 0; i < cart.length; i++) {
        if (cart[i].name == foodName) {
            //点过
            //只修改数量
            cart[i].number++;
            alert('加入购物车成功！');
            // 订单数不变
            num.innerText = cart.length;
            //更新本地存储
            localStorage.setItem('cart', JSON.stringify(cart));
            return;//提前结束函数
        }
    }

    //如果未点过，则添加
    cart.push({ name: foodName, price: foodPrice, number: 1 });
    alert('加入购物车成功！');

    //更新本地存储
    localStorage.setItem('cart', JSON.stringify(cart));

    // 订单数+1
    num.innerText = cart.length;

}




// 切换到购物车
function show() {
    menu.style.display = 'none';
    shopCart.style.display = 'block';
    console.log('切换');
    //从本地存储中获得购物车的数据
    var cartStr = localStorage.getItem('cart');
    var cart = JSON.parse(cartStr);

    if (cart != null) {
        //清空购物车原有数据
        var rows = tbody.getElementsByTagName('tr');
        for (var j = rows.length - 1; j >= 0; j--) {
            tbody.deleteRow(rows[j]);
        }

        //将本地存储中的数据添加到表格中显示
        for (var i = 0; i < cart.length; i++) {
            var newTr = tbody.insertRow();
            newTr.insertCell().innerHTML = '<input type="checkbox" name="cb" onchange="checkChange()" >';
            newTr.insertCell().innerText = cart[i].name;
            newTr.insertCell().innerText = cart[i].price;
            newTr.insertCell().innerText = cart[i].number;
            newTr.insertCell().innerHTML = '<input type="button" value="删除" onclick="del(this)">';

        }

    }
    or();

}
// 切换到菜单
function ct() {
    menu.style.display = 'block';
    shopCart.style.display = 'none';
}





//删除商品

function del(d) {
    var res = window.confirm('确定删除吗？');
    if (res) {
        //删除表格里的订单
        tbody.deleteRow(d.parentNode.rowIndex);

        //删除本地存储
        //先获得本地存储的数据
        var cartStr = localStorage.getItem('cart');
        var cart = JSON.parse(cartStr);

        //所在行索引  d.parentNode.rowIndex

        //从数组中删除数据
        cart.splice(d.parentNode.rowIndex-1, 1);

        //更新存储
        localStorage.setItem('cart', JSON.stringify(cart));
    }
    // 订单数
    num.innerText = cart.length;

    or();
}





//全选
// function allchoose(all) {
//     if (all.checked) {
//         for (var i = 0; i < tbody.rows.length; i++) {
//             tbody.rows[i].cells[0].children[0].checked == true;
//         }
//     } else {
//         for (var j = 0; j < tbody.rows.length; j++) {
//             tbody.rows[j].cells[0].children[0].checked == false;
//         }
//     }
// }


function allchoose(allch) {
    console.log(allch);
    //获得购物车中其他的复选框
    var cbs = document.getElementsByName('cb');
    for (var i = 0; i < cbs.length; i++) {

        cbs[i].checked = allch.checked;
    }
}



//单个变化
function checkChange() {
    //遍历购物车中其他的复选框，若所有都选中，则全选框选中，否则不选中
    var cbs = document.getElementsByName('cb');
    var flag = true;//假设都选中
    for (var i = 0; i < cbs.length; i++) {
        if (!cbs[i].checked) {//有一个未选中
            flag = false;
            break;
        }
    }
    var allch = document.getElementById('all');
    allch.checked = flag;
}


//删除选中
function chDel() {
    var chAll = window.confirm('是否要删除所有选中的订单');
    if (chAll) {
        for (var i = 0; i < tbody.rows.length; i++) {
            if (tbody.rows[i].cells[0].children[0].checked) {
                //删除该行
                tbody.deleteRow(i);
                i--;
                // 订单数-1
                num.innerText--;
            }
        }
    }
    or();
}

//结算



// window.onload=function(){

// }

function sum() {
    var pay = window.confirm('总共付款', rel.innerText, '元');
    if (pay) {
        for (var i = 0; i < tbody.rows.length; i++) {
            if (tbody.rows[i].cells[0].children[0].checked) {
                //删除
                tbody.deleteRow(tbody.deleteRow[i]);
                i--;
                num.innerText--;
            }
        }
    }
    or();
}
